package com.joinway.mobile.controller;

import org.hibernate.validator.constraints.NotBlank;
import org.jsondoc.core.annotation.Api;
import org.jsondoc.core.annotation.ApiError;
import org.jsondoc.core.annotation.ApiErrors;
import org.jsondoc.core.annotation.ApiMethod;
import org.jsondoc.core.annotation.ApiQueryParam;
import org.jsondoc.core.annotation.ApiResponseObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.joinway.common.bean.annotation.LoginName;
import com.joinway.common.bean.annotation.Password;
import com.joinway.framework.bean.constant.ErrorCodeConstants;
import com.joinway.framework.bean.logging.annotation.InputLog;
import com.joinway.framework.bean.logging.annotation.LogIgnore;
import com.joinway.framework.bean.logging.annotation.LogMask;
import com.joinway.framework.bean.logging.annotation.OutputLog;
import com.joinway.framework.bean.logging.type.LogFormat;
import com.joinway.framework.bean.security.annotation.Login;
import com.joinway.framework.bean.security.annotation.Logout;
import com.joinway.framework.bean.view.View;
import com.joinway.framework.core.controller.ExceptionController;
import com.joinway.framework.core.repository.FrameworkRepository;
import com.joinway.framework.support.audit.annotation.Audit;
import com.joinway.framework.support.input.annotation.Render;
import com.joinway.framework.support.input.type.Case;
import com.joinway.framework.support.validation.constraints.CellPhone;
import com.joinway.mobile.bean.view.LoginView;
import com.joinway.mobile.bean.view.VersionView;
import com.joinway.mobile.service.MobileService;

@Api(name = "手机通用接口", description = "手机 用户登录，登出")
@RestController
@RequestMapping("")
@Validated
public class MobileController extends ExceptionController {
	
	private final static Logger log = LoggerFactory.getLogger(MobileController.class);
	
	@Autowired MobileService service;
	
	@Autowired FrameworkRepository repository;
	
	@ApiMethod(description="用户注册", produces=MediaType.APPLICATION_JSON_VALUE)
	@ApiResponseObject
//	@ApiErrors(apierrors={
//			@ApiError(code=MobileErrorConstants.RepeatRegister.Code, description=MobileErrorConstants.RepeatRegister.Description)
//			, @ApiError(code=ErrorCodeConstants.INTERNAL_ERROR, description=ErrorCodeConstants.INTERNAL_ERROR_DESC)
//			, @ApiError(code=ErrorCodeConstants.INVALID_INPUT, description=ErrorCodeConstants.INVALID_INPUT_DESC)
//		}
//	)
	@RequestMapping(value="register.json", method=RequestMethod.POST, produces=MediaType.APPLICATION_JSON_VALUE)
	@Login(name="loginName")
	@Audit
	@InputLog
	@OutputLog(format=LogFormat.Json)
	public LoginView register(
			@LoginName @RequestParam("loginName") @ApiQueryParam(name="loginName", description="用户名,6-20位") @LogMask String loginName
			, @Password @RequestParam("password") @ApiQueryParam(name="password", description="密码,6-20位") @LogIgnore String password
			, @CellPhone @RequestParam("cellPhone") @Render @ApiQueryParam(name="cellPhone", description="手机号") @LogMask String cellPhone
			, @NotBlank @RequestParam("cellPhoneType") @Render(format=Case.Upper) @ApiQueryParam(name="cellPhoneType", description="手机类型", allowedvalues={"I-IOS","A-Android"}) String cellPhoneType
	) throws Exception {
		return service.register(loginName, password, cellPhone, cellPhoneType);
	}

	@ApiMethod(description="用户登录", produces=MediaType.APPLICATION_JSON_VALUE)
	@ApiResponseObject
//	@ApiErrors(apierrors={
//			@ApiError(code=MobileErrorConstants.InvalidUserNameOrPassword.Code, description=MobileErrorConstants.InvalidUserNameOrPassword.Description)
//			, @ApiError(code=ErrorCodeConstants.INTERNAL_ERROR, description=ErrorCodeConstants.INTERNAL_ERROR_DESC)
//			, @ApiError(code=ErrorCodeConstants.INVALID_INPUT, description=ErrorCodeConstants.INVALID_INPUT_DESC)
//		}
//	)
	@RequestMapping(value="login.json", method=RequestMethod.POST, produces=MediaType.APPLICATION_JSON_VALUE)
	@Login
	@Audit
	@InputLog
	@OutputLog(format=LogFormat.Json)
	public LoginView login(
			@LoginName @RequestParam("loginName") @ApiQueryParam(name="loginName", description="用户名,6-20位") @LogMask String loginName
			, @Password @RequestParam("password") @ApiQueryParam(name="password", description="密码,6-20位") @LogIgnore String password
			, @NotBlank @RequestParam("cellPhoneType") @Render(format=Case.Upper) @ApiQueryParam(name="cellPhoneType", description="手机类型", allowedvalues={"I-IOS","A-Android"}) String cellPhoneType
	) throws Exception {
		return service.login(loginName, password, cellPhoneType);
	}

	@ApiMethod(description="用户注销", produces=MediaType.APPLICATION_JSON_VALUE)
	@ApiResponseObject
	@ApiErrors(apierrors={
			@ApiError(code=ErrorCodeConstants.INTERNAL_ERROR, description=ErrorCodeConstants.INTERNAL_ERROR_DESC)
		}
	)
	@RequestMapping(value="logout.json", produces=MediaType.APPLICATION_JSON_VALUE)
	@Logout
	@Audit
	@InputLog
	@OutputLog(format=LogFormat.Json)
	public View logout(@RequestParam(value="userId", required=false, defaultValue="0") @ApiQueryParam(name="userId", description="用户id", required=false) int userId) throws Exception {
		return new View();
	}

	@ApiMethod(description="查看最新版本", produces=MediaType.APPLICATION_JSON_VALUE)
	@ApiResponseObject
	@ApiErrors(apierrors={
			@ApiError(code=ErrorCodeConstants.INTERNAL_ERROR, description=ErrorCodeConstants.INTERNAL_ERROR_DESC)
		}
	)
	@RequestMapping(value="version.json", produces=MediaType.APPLICATION_JSON_VALUE)
	@Audit
	@InputLog
	@OutputLog(format=LogFormat.Json)
	public VersionView version() throws Exception {
		return service.getLatestVersion();
	}

	@ApiMethod(description="修改密码", produces=MediaType.APPLICATION_JSON_VALUE)
	@ApiResponseObject
//	@ApiErrors(apierrors={
//			@ApiError(code=MobileErrorConstants.InvalidUserNameOrPassword.Code, description=MobileErrorConstants.InvalidUserNameOrPassword.Description)
//			, @ApiError(code=ErrorCodeConstants.INTERNAL_ERROR, description=ErrorCodeConstants.INTERNAL_ERROR_DESC)
//			, @ApiError(code=ErrorCodeConstants.INVALID_INPUT, description=ErrorCodeConstants.INVALID_INPUT_DESC)
//		}
//	)
	@RequestMapping(value="password.json", method=RequestMethod.POST, produces=MediaType.APPLICATION_JSON_VALUE)
	@Audit
	@InputLog
	@OutputLog(format=LogFormat.Json)
	public View password(
			@RequestParam("loginName") @Render(format=Case.Lower) @ApiQueryParam(name="loginName", description="用户名") @LogMask String loginName
			, @Password @RequestParam("oldPassword") @ApiQueryParam(name="oldPassword", description="旧密码") @LogIgnore String oldPassword
			, @Password @RequestParam("newPassword") @ApiQueryParam(name="newPassword", description="新密码") @LogIgnore String newPassword
	) throws Exception {
		service.changePassword(loginName, oldPassword, newPassword);
		
		return new View();
	}

	@ApiMethod(description="清除缓存", produces=MediaType.APPLICATION_JSON_VALUE)
	@ApiResponseObject
	@RequestMapping(value="cache/clear.json", produces=MediaType.APPLICATION_JSON_VALUE)
	public View clearCache(){
		repository.clearSystemConfig();
		return new View();
	}
}
